<?php
class setting extends Admin {

    public $tab = array( 'basic' => '常规', 'close' => '开关', 'core' => '核心' );
    private $type = null;

    public function index( $c ) {
        $this->type = R( 'type', 'basic' );

        if ( $this->setting[ 'is_rewrite' ] ) {
            $this->tab[ 'rewrite' ] = '伪静态';
        }
        include $this->display( $c );
    }

    public function save() {
        foreach ( $_POST[ 'setting' ] as $key => $val ) {
            $val = $this->model->escape( trim( $val ) );
            $sql = "REPLACE INTO " . $this->model->t( 'setting' ) . " VALUES ('{$key}', {$val})";
            $this->model->runSql( $sql );
        }

        if ( isset( $_POST[ 'rewrite' ] ) ) {
            $val = serialize( $_POST[ 'rewrite' ] );
            $sql = "REPLACE INTO " . $this->model->t( 'setting' ) . " VALUES ('rewrite', '{$val}')";
            $this->model->runSql( $sql );

            $host = ltrim( S( "HTTP_HOST" ), "www." );
            $this->htaccess( $host );
            $this->nginx( $host );
        }

        $this->model->Cache()->update( 'setting' );
        $this->success( '保存设置成功!' );
    }

    private function htaccess( $host ) {

        $host = str_replace( ".", "\.", $host );

        $route = "<IfModule mod_rewrite.c>\n";
        $route .= "RewriteEngine On\n";
        if ( $_POST[ 'setting' ][ 'rewrite_301' ] ) {
            $route .= "RewriteCond %{HTTP_HOST} ^{$host}$ [NC]\n";
            $route .= "RewriteRule ^(.*)$ http://www\.{$host}/$1 [L,R=301]\n";
        }
        $route .= "RewriteCond %{REQUEST_FILENAME} !-f\n";
        $route .= "RewriteCond %{REQUEST_FILENAME} !-d\n";

        $suffix = str_replace( ".", "\.", $this->setting[ 'rewrite_suffix' ] );
        foreach ( $this->setting[ 'rewrite' ] as $k => $v ) {
            $parment = strpos( $v, "\$alias" ) !== false ? "alias" : 'id';
            $pattern = str_replace( array( '$alias', '$id', '$key', '$tag' ),
                array(
                    "([^{$this->setting[ 'rewrite_sep' ]}]+)", "([0-9]+)",
                    "([^{$this->setting[ 'rewrite_sep' ]}]+)",
                    "([^{$this->setting[ 'rewrite_sep' ]}]+)" ), $v );
            $pattern = str_replace( array( '$y', '$m', '$d' ), "[0-9]+", $pattern );
            $route .= "RewriteRule ^{$pattern}{$this->setting[ 'rewrite_sep' ]}?([0-9]+)?{$suffix}$ index.php?c={$k}&{$parment}=$1&page=$2 [L]\n";
        }
        $route .= "RewriteRule ^page/([0-9]+)?{$suffix}$ index.php?action=main&page=$1 [L]\n";
        $route .= "RewriteRule ^([^/]+)/?([0-9]+)?{$suffix}$ index.php?c=archives&alias=$1&page=$2 [L]\n";
        $route .= "</IfModule>";
        $route_file = APP_PATH . "/.htaccess";

        if ( is_readable( $route_file ) ) @unlink( $route_file );
        file_put_contents( $route_file, $route );
        chmod( $route_file, 0666 );
    }

    private function nginx( $host ) {
        $route = "location / {\n";
        if ( $_POST[ 'setting' ][ 'rewrite_301' ] ) {
            $route .= "\tif (\$host != 'www.{$host}' ) {\n";
            $route .= "\t\trewrite ^/(.*)$ http://www.{$host}/$1 permanent;\n";
            $route .= "\t}\n";
        }
        $route .= "\tif (-f \$request_filename/index.html){\n";
        $route .= "\t\trewrite (.*) $1/index.html break;\n";
        $route .= "\t}\n";
        $route .= "\tif (-f \$request_filename/index.php){\n";
        $route .= "\t\trewrite (.*) $1/index.php;\n";
        $route .= " \t}\n";
        $route .= "\tif (!-f \$request_filename){\n";
        $route .= "\t\trewrite (.*) /index.php;\n";
        $route .= "\t}\n";
        $route .= "}";

        $route_file = APP_PATH . "/nginx.conf";
        file_put_contents( $route_file, $route );
        chmod( $route_file, 0666 );
    }

    private function get_time_zone() {
        return array( 'Kwajalein' => '日界线西', 'Pacific/Midway' => ' 中途岛、萨摩亚群岛', 'Pacific/Honolulu' => '夏威夷', 'America/Anchorage' => '阿拉斯加', 'America/Los_Angeles' => '太平洋时间(美国和加拿大)', 'America/Denver' => '山地时间(美国和加拿大)', 'America/Tegucigalpa' => '中部时间(美国和加拿大)、墨西哥城', 'America/New_York' => '东部时间(美国和加拿大)、波哥大', 'America/Halifax' => '大西洋时间(加拿大)、加拉加斯', 'America/St_Johns' => '纽芬兰', 'America/Sao_Paulo' => '巴西、布宜诺斯艾利斯、乔治敦', 'Atlantic/South_Georgia' => '中大西洋', 'Atlantic/Azores' => '亚速尔群岛、佛得角群岛', 'Europe/Dublin' => '西欧时间、伦敦、卡萨布兰卡', 'Europe/Belgrade' => '中欧时间、安哥拉、利比亚', 'Europe/Minsk' => '东欧时间、开罗，雅典', 'Asia/Kuwait' => '巴格达、科威特、莫斯科', 'Asia/Tehran' => '德黑兰', 'Asia/Muscat' => '阿布扎比、马斯喀特、巴库', 'Asia/Yekaterinburg' => '叶卡捷琳堡、伊斯兰堡、卡拉奇', 'Asia/Kolkata' => '孟买、加尔各答、新德里', 'Asia/Dhaka' => '阿拉木图、 达卡、新亚伯利亚', 'Asia/Krasnoyarsk' => '曼谷、河内、雅加达', 'Asia/Brunei' => '北京、重庆、香港、新加坡', 'Asia/Seoul' => '东京、汉城、大阪、雅库茨克', 'Australia/Darwin' => '阿德莱德、达尔文', 'Australia/Canberra' => '悉尼、关岛', 'Asia/Magadan' => '马加丹、索罗门群岛', 'Pacific/Fiji' => '奥克兰、惠灵顿、堪察加半岛', );
    }

    private function get_time_style() {
        return array( 'Y-m-d H:i:s' => '年-月-日 时:分:秒 ', 'm-Y-d H:i:s' => '月-年-日 时:分:秒 ', 'd-m-Y H:i:s' => '日-月-年 时:分:秒 ', );
    }

    private function get_upload_style() {
        return array( 'Ymd' => '年月日', 'Y-m-d' => '年-月-日', 'Y/m/d' => '年/月/日', );
    }
}